const http = require("http");
const url = require("url");
const querystring = require("querystring");

const server = http.createServer((req, res) => {
  const parsedUrl = url.parse(req.url, true);

  if (parsedUrl.pathname === "/my-jsonp") {
    // const callbackName = queryData.callback;
    // const data = queryData.data;

    // 设置响应头
    res.setHeader("Content-Type", "application/javascript");

    // 构造 JSONP 响应内容
    const _f = /callback=(.*?)[&]*$/g.exec(req.url); //   callback=tmp_0df0608773855
    const data = `今天中午吃红烧肉`;
    const responseContent = `globalThis.${_f[1]}("${data}")`;
    res.end(responseContent);
  } else {
    res.statusCode = 404;
    res.end("Not Found");
  }
});

const port = 6666;
server.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
